// https://leetcode-cn.com/problems/search-a-2d-matrix-ii/
// 240. Search a 2D Matrix II
// Write an efficient algorithm that searches for a value target in an m x n
// integer matrix matrix. This matrix has the following properties:
// - Integers in each row are sorted in ascending from left to right
// - Integers in each column are sorted in ascending from top to bottom
function searchMatrix(matrix: number[][], target: number): boolean {
    if (!matrix || matrix.length === 0 || matrix[0].length === 0) return false;
    let i = 0;
    let j = matrix[0].length - 1;
    while (i < matrix.length && j >= 0) {
        if (matrix[i][j] === target) return true;
        else if (matrix[i][j] < target) {
            i++;
        } else {
            j--;
        }
    }

    return false;
}
